<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/4/9
 * Time: 11:04
 */

namespace app\index\controller;

use function PHPSTORM_META\type;
use think\Controller;
use app\index\controller\BootstrapZs7y;

class Department extends Controller
{
//    public function isempty($data)
//    {
//        if (!$data['data']) {
//          $data['data'][0]['id']='';
//        }
//        return $data;
//    }

    public function cdx()
    {
        $this->redirect('../Department/department_nav');   //重定向

    }

    public function _empty()
    {
        $this->redirect('../Department/department_nav');   //空操作 解决用户恶意的输入 报错影响交互
    }

    public function department()
    {
        $A = new Api();
        if (!input('get.cid')) {
            $this->redirect('../Department/department_nav');
        }
        if (input('get.cid') == '63414925-4e1c-4b61-9f66-fd2d4f8078ea') {//科研跳转到科研主页
            $this->redirect('../Research/scientific_research');
        }
        $data = $A->get_detail('departments', input('get.cid'), 0);
        $data = getImg($data);
        $this->assign("introduce", $data['data']);
        $profession = $A->get_c_d_article('fb3a5e13-dc08-478b-ac52-fd86fa48c393', input('get.cid'), 0, 5);
        $profession = getImg($profession);

        $development = $A->get_c_d_article('c22b05d1-ece8-4714-8d67-76313433b113', input('get.cid'), 0, 4);
        $health = $A->get_c_d_article('50982d10-7187-443b-a464-ae3998e55ff1', input('get.cid'), 0, 5);
        $team = $A->get_d_d_detail(input('get.cid'));
        $team = array_slice($team, 0, 6);
        $team = getImg($team);

        $this->assign(["team" => $team['data'],
            "profession" => $profession['data'],
            "development" => [
                'list' => $development['data'],
                'id' => 'c22b05d1-ece8-4714-8d67-76313433b113'
            ],
            "health" => [
                'list' => $health['data'],
                'id' => '50982d10-7187-443b-a464-ae3998e55ff1'
            ]
        ]);
        //科室导航条
        $departmentlist = [];
        $data_list = $A->get_menu('departments');
        for ($i = 0; $i < count($data_list['data']); $i++) {
            if ($data_list['data'][$i]['id'] == input('get.cid')) {
                $departmentlist = [
                    'name' => $data_list['data'][$i]['attributes']['name'],
                    'id' => $data_list['data'][$i]['id'],
                ];
            }
        }
        $this->assign("departmentlist", $departmentlist);
        $this->assign("web_title", $departmentlist['name']);
        return view();
    }

    public function department_nav()
    {
        //获取科室分类
        $cates = [
            ['id' => '2728580c-06fc-4169-ad43-9ec1408f99f0', 'type' => 'department'],//诊疗中心
            ['id' => 'f7d007e6-401d-45c5-9919-72f12b94ca6d', 'type' => 'department'],//非手术科室
            ['id' => '5884bf44-192c-4113-9a59-1c840601f55b', 'type' => 'department'],//手术科室
            ['id' => '693e9cc3-7ff6-43b5-a376-795a590ddf5a', 'type' => 'department'],//平台科室
        ];
        $A = new Api();
        $n = 0;
        $j = 0;

        $_data = $A->get_menu('departments');
        $_data = getImg($_data);
        foreach ($cates as $key => $value) {

            for ($i = 0; $i < count($_data['data']); $i++) {
                if ($_data['data'][$i]['relationships']['parent']['data']) {
                    if ($_data['data'][$i]['relationships']['parent']['data'][0]['id'] == $value['id']) {

                        $department2[$n] = [
                            'id' => $_data['data'][$i]['id'],
                            'name' => $_data['data'][$i]['attributes']['name']
                        ];
                        $n++;
                    }
                }
            }
            $n = 0;
            $key = 'department' . ($j + 1);
            $j++;
            $data[$key] = [
                'id' => $value['id'],
                'list' => $department2,
            ];
        }
        $this->assign($data);
        $this->assign("web_title", "科室导航");
        return view();
    }

    public function department_doctors()  //获取指定科室的所有医生
    {
        $A = new Api();
        //科室导航条
        $departmentlist = [];
        $is_plat = input('get.pid')!=null;
        if ($is_plat) {
            $data_list = $A->get_menu('platforms');
            $id_using = input('get.pid');
        }
        else {
            $data_list = $A->get_menu('departments');
            $id_using = input('get.cid');
        }
        for ($i = 0; $i < count($data_list['data']); $i++) {
            if ($data_list['data'][$i]['id'] == $id_using) {
//                科研中心单独处理名字
                if ($id_using == '63414925-4e1c-4b61-9f66-fd2d4f8078ea') {
                    $name = '科研';
                } else {
                    $name = $data_list['data'][$i]['attributes']['name'];
                }
                $departmentlist = [
                    'name' => $name,
                    'id' => $data_list['data'][$i]['id'],
                ];
            }
        }
        if ($is_plat) {
            $doctors = $A->get_platform_doctor($id_using);
        } else {
            $doctors = $A->get_d_d_detail($id_using);
        }
        $doctors = getImg($doctors);
        $count = count($doctors['data']);
        $page = input('get.page');//分页
        $listRow = 10;
        if (isset($page) && null !== $page && $page * $listRow <= count($doctors['data'])) {
            $currentPage = $page; // 当前页
        } else if ($page * $listRow > count($doctors['data'])) {
            $currentPage = ceil(count($doctors['data']) / $listRow);
        } else {
            $currentPage = 1;
        }
        $bootstrap = BootstrapZs7y::make($doctors, $listRow, $currentPage, count($doctors['data']), false, ['path' => url('department/department_doctors'), 'query' => ['cid' => input('get.cid')]]);
        $doctors['data'] = array_slice($doctors['data'], ($currentPage - 1) * $bootstrap->listRows(), $bootstrap->listRows());
        $this->assign([
            'is_plat' => $is_plat,
            'departmentlist' => $departmentlist,
            'doctors' => $doctors['data'],
            'count' => $count,
            'bootstrap' => $bootstrap->render()
        ]);
        $this->assign("web_title", "科室医生");

        return view();
    }

    public function department_doctor_detail() //医生详情页
    {
        $A = new Api();

        $doctor = $A->get_detail('doctors', input('get.id'), 0);//获取医生信息
        $doctor = getImg($doctor);
        $departmentlist = []; //科室导航条
        $data_list = $A->get_menu('departments');
        for ($i = 0; $i < count($data_list['data']); $i++) {
            if ($data_list['data'][$i]['id'] == $doctor['data'][0]['relationships']['department']['data']['id']) {
                if ($data_list['data'][$i]['id'] == '63414925-4e1c-4b61-9f66-fd2d4f8078ea') {
                    $name = '科研';
                } else {
                    $name = $data_list['data'][$i]['attributes']['name'];
                }
                $departmentlist = [
                    'name' => $name,
                    'id' => $data_list['data'][$i]['id'],
                ];
            }
        }
        if (!$departmentlist) {
            $departmentlist = [
                'name' => '',
                'id' => '',
            ];
        }
        $this->assign([
            'departmentlist' => $departmentlist,
            'doctor' => $doctor['data'],
        ]);
        $this->assign("web_title", "医生详情");
        return view();
    }

    public function department_nav_detail()
    {  //科室导航里的二级栏目
        $A = new Api();
        $dar1 = '';//一级栏目
        $dar2 = '';//二级栏目
        $is_plat = input('get.platform')!=null;
        if (!$is_plat) {
            $cates = [
                ['type' => 'departments', 'id' => input('get.department')],
            ];
        } else {
            $cates = [
                ['type' => 'platforms', 'id' => input('get.platform')],
            ];
        }
        $data2 = $A->get_menu('columns');
        $column = $data2['data'];
        for ($i = 0; $i < count($column); $i++) {
            if ($column[$i]['id'] == input('get.column')) {
                $column = [
                    'id' => input('get.column'),
                    'name' => $column[$i]['attributes']['name']
                ];
            }
        }
        //找不到栏目时候
        if (!isset($column['name'])) {
            $column = [
                'id' => '',
                'name' => '未找到此栏目'
            ];
        }
        foreach ($cates as $k => $v) {

            $_data = $A->get_detail($v['type'], $v['id'], 0);
            $_data[$k] = [
                'id' => $_data['data'][$k]['id'],
                'name' => $_data['data'][$k]['attributes']['name']
            ];
        }
        if (!$is_plat) {
            $data = $A->get_c_d_article(input('get.column'), input('get.department'), 0, 100);
        } else {
            $data = $A->get_platform_article(input('get.platform'), COLUMNS[input('get.column')], 0, UNLIMITED_NUM);
        }
        $data = getImg($data);
        $page = input('get.page');//分页
        $listRow = 5;
        if (isset($page) && null !== $page && $page * $listRow <= count($data['data'])) {
            $currentPage = $page; // 当前页
        } else if ($page * $listRow > count($data['data'])) {
            $currentPage = ceil(count($data['data']) / $listRow);
        } else {
            $currentPage = 1;
        }
        $bootstrap = BootstrapZs7y::make($data, $listRow, $currentPage, count($data['data']), false, ['path' => url('department/department_nav_detail'), 'query' => ['column' => input('get.column'), 'department' => input('get.department'), 'platform' => input('get.platform')]]);
        $data['data'] = array_slice($data['data'], ($currentPage - 1) * $bootstrap->listRows(), $bootstrap->listRows());
        $this->assign([
            'is_plat' => $is_plat,
            'departmentlist' => $_data[0],
            'data' => $data['data'],
            'column' => $column,
            'bootstrap' => $bootstrap->render(),
            'web_title' => COLUMNS[input('get.column')]
        ]);
        if (input('get.column') == '50982d10-7187-443b-a464-ae3998e55ff1' || input('get.column')==array_search('科研成果', COLUMNS)) {
            return $this->fetch("department/department_normal");
        } else {
            return $this->fetch("department/department_img");
        }
    }
}
